package com.sihai.ehr.web.action.sign;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.FinancialPaymentApplyForm;
import com.sihai.ehr.model.biz.FinancialPaymentApplyFormAuditing;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.FinancialPaymentApplyFormService;
import com.sihai.ehr.service.UserService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.List;

/**
 * author: wxn
 * since: 13-4-14 下午12:32
 * 财务签核系统 - 用款申请单Action
 */
@Results({
        @Result(name = "list", location = "/WEB-INF/pages/sign/financial_payment_apply_form_list.jsp"),
        @Result(name = "add", location = "/WEB-INF/pages/sign/financial_payment_apply_form_input.jsp"),
        @Result(name = "audit", location = "financial_payment_apply_form_audit.jsp")
})
public class FinancialPaymentApplyFormAction extends SignBaseAction {
    @BeanInject
    private FinancialPaymentApplyFormService financialPaymentApplyFormService;
    @BeanInject
    private UserService userService;

    private LimitedList<FinancialPaymentApplyForm> financialPaymentApplyForms;
    private FinancialPaymentApplyForm financialPaymentApplyForm;

    private List<List<FinancialPaymentApplyFormAuditing>> audits;

    @Action("/public/financial-payment-apply-form")
    public String listByUser() throws Exception {
        userType = 2;
        String temp = execute();
        showChildOrg = false;
        return temp;
    }

    @Action("/sign/financial-payment-apply-form")
    @Override
    public String execute() throws Exception {
        showChildOrg = true;
        init();
        User user = null;
        if(userType == 2){
            user = userService.getUser(getCurrentUserId());
        }
        financialPaymentApplyForms = financialPaymentApplyFormService.findFinancialPaymentApplyForm(user, searchFormPojo, createLimit(), createSorter());
        this.paging(financialPaymentApplyForms);
        if(userType == 1){
            this.setPageUrl("sign/financial-report-form.xhtml");
        }else if(userType == 2){
            this.setPageUrl("public/financial-report-form.xhtml");
        }
        return "list";
    }

    @Action("/public/financial-payment-apply-form-input")
    public String toAddByUser() throws Exception {
        userType = 2;
        return "add";
    }

    @Action("/sign/financial-payment-apply-form-input")
    public String toAdd() throws Exception {
        return "add";
    }

    @Action("/public/financial-payment-apply-form-save")
    public String saveByUser() throws Exception {
        userType = 2;
        return save();
    }

    //保存
    @Action("/sign/financial-payment-apply-form-save")
    public String save() throws Exception {
        if(financialPaymentApplyForm != null){
            User user = userService.getUser(getCurrentUserId());
            financialPaymentApplyForm.setWork(user != null ? user.getWork() : null);
            financialPaymentApplyForm.setStatus(AuditStatus.wait_audit);
            financialPaymentApplyFormService.save(financialPaymentApplyForm);
        }
        return execute();
    }

    @Action("/sign/financial-payment-apply-form-audit")
    public String toAudit() throws Exception {
        //得到审核对象
        if(financialPaymentApplyForm == null || financialPaymentApplyForm.getId() == null){
            return execute();
        }
        financialPaymentApplyForm = financialPaymentApplyFormService.get(financialPaymentApplyForm.getId());

        //得到当前审核历史记录(没有就先保存一个审核流程次数)
        audits = financialPaymentApplyFormService.findFinancialPaymentApplyFormAuditing(financialPaymentApplyForm.getId());
        if(audits == null || audits.isEmpty()){
            financialPaymentApplyFormService.saveFinancialPaymentApplyFormAuditingProcess(financialPaymentApplyForm.getId());
            audits = financialPaymentApplyFormService.findFinancialPaymentApplyFormAuditing(financialPaymentApplyForm.getId());
        }
        //检查当前登录用户是否可以审核当前结点
        if(financialPaymentApplyForm.getWork() != null && audits != null && !audits.isEmpty()){
            checkCanAudit(financialPaymentApplyForm.getWork().getDuty(), audits.get(audits.size()-1));
        }
        currentAuditType = AuditType.audit_financial_payment_apply_form.toString();

        return "audit";
    }

    public FinancialPaymentApplyForm getFinancialPaymentApplyForm() {
        return financialPaymentApplyForm;
    }

    public void setFinancialPaymentApplyForm(FinancialPaymentApplyForm financialPaymentApplyForm) {
        this.financialPaymentApplyForm = financialPaymentApplyForm;
    }

    public LimitedList<FinancialPaymentApplyForm> getFinancialPaymentApplyForms() {
        return financialPaymentApplyForms;
    }

    public List<List<FinancialPaymentApplyFormAuditing>> getAudits() {
        return audits;
    }
}
